package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.IndustryMapInfo;
import cc.rengu.redp.bizimpl.enums.PayChannelEnum;
import cc.rengu.redp.bizimpl.upmp.service.IndustryMapInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:行业类别映射表管理")
@RestController
@RequestMapping("/admin/merParam/industryMapInfo")
public class IndustryMapInfoController extends BaseController {
    @Autowired
    private IndustryMapInfoService industryMapInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, IndustryMapInfo entity) {
        return getDataTable(this.industryMapInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public IndustryMapInfo getOne(@NotBlank(message = "{required}") String id) {
        return industryMapInfoService.getById(id);
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "industryType", value = "行业类别编码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "destChannel", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "destCatalogCd", value = "经营类目编码", required = true, dataType = "String")
    })
    public void add(@Valid IndustryMapInfo entity) throws RedpException {
        try {
            this.industryMapInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid IndustryMapInfo entity) throws RedpException {
        try {
            this.industryMapInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.industryMapInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.industryMapInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "destChannel", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "destCatalogCd", value = "经营类目",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "publicPrivateFlag", value = "对公私专用标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "industryType", value = "行业代码", required = true, dataType = "String")
    })
    public RedpResponse check(String id,
                              @NotBlank(message = "{required}") String industryType,
                              @NotBlank(message = "{required}") String destChannel,
                              String destCatalogCd,
                              @NotBlank(message = "{required}") String publicPrivateFlag
    ) throws RedpException {
        LambdaQueryWrapper<IndustryMapInfo> wrapper = new LambdaQueryWrapper();
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(IndustryMapInfo::getId, id);
            }
            if (PayChannelEnum.WXZF.getType().equals(destChannel)){
                //微信
                wrapper.eq(IndustryMapInfo::getIndustryType, industryType);
                wrapper.eq(IndustryMapInfo::getDestChannel, destChannel);
                wrapper.eq(IndustryMapInfo::getPublicPrivateFlag, publicPrivateFlag);
                IndustryMapInfo info = industryMapInfoService.getOne(wrapper);
                wrapper = new LambdaQueryWrapper();
                if (StringUtils.checkValNotNull(id)) {
                    wrapper.ne(IndustryMapInfo::getId, id);
                }
                wrapper.eq(IndustryMapInfo::getIndustryType, industryType);
                wrapper.eq(IndustryMapInfo::getDestChannel, destChannel);
                wrapper.eq(IndustryMapInfo::getDestCatalogCd, destCatalogCd);
                IndustryMapInfo info1 = industryMapInfoService.getOne(wrapper);
                if (null != info||null != info1) {
                    throw new RedpException("[ 行业类别映射关系 ] 重复，请重新输入");
                }
            }else{
                //支付宝
                wrapper.eq(IndustryMapInfo::getIndustryType, industryType);
                wrapper.eq(IndustryMapInfo::getDestChannel, destChannel);
                IndustryMapInfo info = industryMapInfoService.getOne(wrapper);
                if (null != info) {
                    throw new RedpException("[ 行业类别映射关系 ] 重复，请重新输入");
                }
            }
        return new RedpResponse().message("验证成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "条件", dataType = "String")
    })
    @GetMapping("/destCatalogCdSelect2List")
    public Map<String, Object> unionMerGrpSelect2List(QueryRequest request, String q) {
        return getDataTable(this.industryMapInfoService.destCatalogCdSelect2List(request, q));
    }
}
